<?php
/**
 * @name Factory
 *
 * LICENSE
 *
 * This source file is subject to the BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.phpxe.com/license/
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to Chaodong.Sheng <scd113@gmail.com> so we can send you a copy immediately.
 *
 * @category   Assemble of Scd Framework
 * @package    Scd
 * @copyright  Copyright (c) 2007 PHPXe Web Studio <http://www.phpxe.com>
 * @license    http://framework.phpxe.com/license/
 * @version    $Id: Factory.php Thu Sep 20 01:29:18 CST 2007 Davy $
 */

class Scd_Factory {
	
	var $_config = null;

	function Scd_Factory( $objconf = null ) {
		$this->getConfig( &$objconf );
	}

	function getConfig( $objconf = null )
	{
		if (is_object($objconf)) {
			$this->_config = ($config === null) ? new Sonfig() : $objconf;
		}
		return $this;
	}

	/**
	 * Get a language object
	 *
	 * Returns a reference to the global {@link JLanguage} object, only creating it
	 * if it doesn't already exist.
	 *
	 * @access public
	 * @return object JLanguage
	 */
	function &getLanguage()
	{
		static $instance;

		if (!is_object($instance)) {
			$instance = Scd_Factory::_createLanguage();
		}

		return $instance;
	}

	/**
	 * Get an authorization object
	 *
	 * Returns a reference to the global {@link JAuthorization} object, only creating it
	 * if it doesn't already exist.
	 *
	 * @access public
	 * @return object JAuthorization
	 */
	function &getACL( )
	{
		static $instance;

		if (!is_object($instance)) {
			$instance = Scd_Factory::_createACL();
		}

		return $instance;
	}

	/**
	 * Get a template object
	 *
	 * Returns a reference to the global {@link JTemplate} object, only creating it
	 * if it doesn't already exist.
	 *
	 * @access public
	 * @return object JTemplate
	 */
	function &getTemplate( )
	{
		static $instance;

		if (!is_object($instance)) {
			$instance = Scd_Factory::_createTemplate();
		}

		return $instance;
	}

	/**
	 * Get a database object
	 *
	 * Returns a reference to the global {@link JDatabase} object, only creating it
	 * if it doesn't already exist.
	 *
	 * @return object JDatabase
	 */
	function &getDBO()
	{
		static $instance;

		if (!is_object($instance))
		{
			$instance = Scd_Factory::_createDBO();
		}

		return $instance;
	}

	/**
	 * Create an ACL object
	 *
	 * @access private
	 * @return object JAuthorization
	 * @since 1.5
	 */
	function &_createACL()
	{
		Scd::loadClass('scd.acl');

		$db =&  Scd_Factory::getDBO();

		$options = array(
			'db'				=> &$db,
			'db_table_prefix'	=> $db->getPrefix() . 'acl_',
			'debug'				=> 0
		);
		$acl = new Scd_Acl( $options );

		return $acl;
	}

	/**
	 * Create an database object
	 *
	 * @access private
	 * @return object JDatabase
	 * @since 1.5
	 */
	function &_createDBO()
	{
		Scd::loadClass("scd.db.select");
		$db = new Scd_Db_Select( $this->_config );
		return $db;
	}

	/**
	 * Create a template object
	 *
	 * @access private
	 * @param array An array of support template files to load
	 * @return object JTemplate
	 * @since 1.5
	 */
	function &_createTemplate()
	{
		if ($this->_config->smarty) {
			Scd::loadClass("scd.view.smarty");
			$tpl = new Scd_View_Smarty( $this->_config );
			return $tpl;
		}
	}

	/**
	 * Create a language object
	 *
	 * @access private
	 * @return object JLanguage
	 * @since 1.5
	 */
	function &_createLanguage()
	{
		return $lang;
	}
}